#ifndef _LOG_H_
#define _LOG_H_

#include <inttypes.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <assert.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>


#ifdef __cplusplus
extern "C" {
#endif


enum {
	LEVEL_NONE = -1,
	LEVEL_MIN = 0,
	LEVEL_TRACE = 0,
	LEVEL_DEBUG = 1,
	LEVEL_INFO = 2,
	LEVEL_WARN = 3,
	LEVEL_ERROR = 4,
	LEVEL_FATAL = 5,
	LEVEL_MAX = 5,
};

int set_log_level(int level);

int logv(int level, const char *fmt, va_list ap);


int log_write(int level, const char *fmt, ...);


#ifdef NDEBUG
#define log_trace(fmt, args...) \
	do{}while(0)
#else
#define log_trace(fmt, args...) \
    log_write(LEVEL_TRACE, "%s(%d): " fmt, __FILE__, __LINE__, ##args)
#endif


#define log_debug(fmt, args...) \
    log_write(LEVEL_DEBUG, "%s(%d): " fmt, __FILE__, __LINE__, ##args)

#define log_info(fmt, args...) \
    log_write(LEVEL_INFO, "%s(%d): " fmt, __FILE__, __LINE__, ##args)

#define log_warn(fmt, args...) \
    log_write(LEVEL_WARN, "%s(%d): " fmt, __FILE__, __LINE__, ##args)

#define log_error(fmt, args...) \
    log_write(LEVEL_ERROR, "%s(%d): " fmt, __FILE__, __LINE__, ##args)

#define log_fatal(fmt, args...) \
    log_write(LEVEL_FATAL, "%s(%d): " fmt, __FILE__, __LINE__, ##args)



#ifdef __cplusplus
}
#endif

#endif
